package tum0r.cryptology.decryption;

import tum0r.misc.StringUtils;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.cryptology.decryption<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/22 20:20<br>
 * <br>
 */
public class AffineDecryption {
    public String decryption(String cipherText, int m, int n) {
        StringBuilder plainText = new StringBuilder();
        if(!StringUtils.isNullOrEmptyOrSpace(cipherText)) {
            char[] temp = cipherText.toCharArray();
            for (int count = 0; count <= 26; count++) {
                if ((m * count) % 26 == 1) {
                    m = count;
                }
            }
            for (char c : temp) {
                if ((c >= 65 && c <= 90)) {
                    plainText.append((char) (((c - 'A' - n + 26) * m) % 26 + 'A'));
                } else if ((c >= 97 && c <= 122)) {
                    plainText.append((char) (Math.abs(((c - 'a' - n + 26) * m) % 26) + 'a'));
                }
            }
        }
        return plainText.toString();
    }
}
